package com.qingdai.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingdai.emuns.Commodity;
import com.qingdai.emuns.Order;
import com.qingdai.emuns.User;
import com.qingdai.entities.CommonResult;
import com.qingdai.service.OrderService;
import com.qingdai.service.UserService;
import com.qingdai.utils.JwtUtil;
import com.qingdai.utils.TimeUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.net.http.HttpRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/order")
@Tag(name = "订单")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private TimeUtil timeUtil;
    @Resource
    private UserService userService;

    @PostMapping("/es")
    @Operation(summary = "所有订单存到ES", description = "")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<List<Order>> addAddressesToES() {
        List<Order> allOrders = orderService.list();
        Iterable<Order> ordersIterable = orderService.saveOrders(allOrders);
        return new CommonResult<>(200, "所有订单存到ES成功");
    }

    //获取所有订单信息
    @GetMapping("/all/xyz")
    @Operation(summary = "获取所有订单", description = "获取所有订单")
    public CommonResult getAllCarts() {
        List<Order> allCarts = orderService.list();
        return new CommonResult(200, "获取所有订单成功", allCarts);
    }

    @GetMapping("/jwt")
    @Operation(summary = "用户获取自己订单", description = "用户获取自己订单")
    public CommonResult getCartsByJwt(HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        LambdaQueryWrapper<Order> w = new LambdaQueryWrapper<>();
        w.eq(Order::getBuyerId, jwtUserId);
        List<Order> allCarts = orderService.list(w);
        return new CommonResult(200, "获取所有订单成功", allCarts);
    }


    //创建新订单
    @PostMapping("/jwt")
    @Operation(summary = "创建新订单", description = "创建新订单")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "deliverAddressId", description = "收货地址id", required = true),
            @Parameter(name = "shopId", description = "店铺id", required = true),
//            @Parameter(name = "buyerId", description = "买家id", required = true),
            @Parameter(name = "commodityIdList", description = "商品id列表", required = true),
            @Parameter(name = "commodityNumberList", description = "商品数量列表", required = true),
//            @Parameter(name = "creationTime", description = "订单创建时间", required = true),
//            @Parameter(name = "paymentTime", description = "订单支付时间", required = true),
//            @Parameter(name = "sendTime", description = "返货时间", required = true),
    })
    public CommonResult createCart(@RequestParam(required = true) Integer deliverAddressId,
                                   @RequestParam(required = true) Integer shopId,
//                                   @RequestParam(required = true) Integer buyerId,
                                   @RequestParam(required = true) String commodityIdList,
                                   @RequestParam(required = true) String commodityNumberList,
//                                   @RequestParam(required = true) String creationTime,
//                                   @RequestParam(required = true) String paymentTime,
//                                   @RequestParam(required = true) String sendTime,
                                   HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        String now = timeUtil.getNow();
        orderService.save(new Order(null, deliverAddressId, shopId, jwtUserId, commodityIdList, commodityNumberList, now, null, null));
        return new CommonResult(200, "创建订单成功");
    }


    //根据id更新订单信息
    @PutMapping("/jwt/id")
    @Operation(summary = "更新订单", description = "根据id更新订单")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "deliverAddressId", description = "收货地址id", required = false),
            @Parameter(name = "shopId", description = "店铺id", required = false),
            @Parameter(name = "buyerId", description = "卖家id", required = false),
            @Parameter(name = "commodityIdList", description = "商品id列表", required = false),
            @Parameter(name = "commodityNumberList", description = "商品数量列表", required = false),
            @Parameter(name = "creationTime", description = "订单创建时间", required = false),
            @Parameter(name = "paymentTime", description = "订单支付时间", required = false),
            @Parameter(name = "sendTime", description = "返货时间", required = false),
    })
    public CommonResult updateOrderById(@RequestParam Integer id,
                                    @RequestParam(required = false) Integer deliverAddressId,
                                    @RequestParam(required = false) Integer shopId,
//                                    @RequestParam(required = false) Integer buyerId,
                                    @RequestParam(required = false) String commodityIdList,
                                    @RequestParam(required = false) String commodityNumberList,
                                    @RequestParam(required = false) String creationTime,
                                    @RequestParam(required = false) String paymentTime,
                                    @RequestParam(required = false) String sendTime,
                                    HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        Order order = orderService.getById(jwtUserId);
        if (order != null) {
            if (!Objects.equals(order.getBuyerId(), jwtUserId)) {
                return new CommonResult(400, "请勿修改他人订单");
            }
            orderService.updateById(new Order(null, deliverAddressId, shopId, jwtUserId, commodityIdList, commodityNumberList, creationTime, paymentTime, sendTime));
            return new CommonResult(200, "更新订单信息成功");
        } else {
            return new CommonResult(404, "订单id非法");
        }
    }

    //根据id获取订单信息
    @GetMapping("/jwt/id")
    @Operation(summary = "根据id获取订单", description = "根据id获取订单")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult updateCartById(@RequestParam(required = true) Integer id,
                                       HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        Order order = orderService.getById(id);
        if (order != null) {
            if (order.getBuyerId() != jwtUserId) {
                return new CommonResult(404, "请勿查询他人订单");
            }
            return new CommonResult(200, "根据id:" + id + ",获取订单成功", order);
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到订单");
        }
    }

    //根据id移除订单信息
    @DeleteMapping("/jwt/id")
    @Operation(summary = "根据id移除订单", description = "根据id移除订单")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult removeCartById(@RequestParam(required = true) Integer id,
                                       HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
        Integer jwtUserId = (Integer) result.getData();
        Order order = orderService.getById(id);
        if (order != null) {
            if (!Objects.equals(order.getBuyerId(), jwtUserId)) {
                return new CommonResult(404, "请勿删除他人订单");
            }
            orderService.removeById(id);
            return new CommonResult(200, "根据id:" + id + ",移除订单成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到订单");
        }
    }

}
